#include <iostream>
#include <unistd.h>
#include <sys/eventfd.h>


//eventfd的测试代码:阻塞模式和非阻塞模式,信号量模式和默认模式
int main()
{
    int fd = eventfd(0,EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE);//默认模式,非阻塞模式
    uint64_t val;
    val = 1;
    write(fd,&val,sizeof(val));
    val = 2;
    write(fd,&val,sizeof(val));

    val = 0;
    int ret = read(fd,&val,sizeof(val));
    std::cout << "读取到的val是:"  << val << std::endl;
    
    val = 0;
    ret = read(fd,&val,sizeof(val));
    std::cout << "读取到的val是:" << val << std::endl;

    val = 0;
    ret = read(fd,&val,sizeof(val));
    std::cout << "读取到的val是:" << val << std::endl;

    val = 0;
    ret = read(fd,&val,sizeof(val));
    std::cout << "读取到的val是:" << val << std::endl;
    if(ret == -1)
        std::cout << "本是阻塞逻辑,设置为非阻塞,返回-1" << std::endl;
    return 0;
}

